package com.yc.oop4;

import java.util.Scanner;

public class Game {
	private Hero hero;
	private int mapIndex = 1;			//第几层
	private int hindex = 82;		//勇士当前所在的位置
	private boolean isGameOver = false;		//游戏是否结束
	private Scanner sc = new Scanner(System.in);
	private String choice;
	//记录一下,勇士在每一层的位置
	private int[] upIndex = new int[] {82, 21, 71};
	private int[] downIndex = {25, 71, 25};
	
	public void startGame() {
		//先实例化
		hero = new Hero();
		System.out.println( hero );
		
		//显示地图
		createMap();
		
		while(!isGameOver) {
			System.out.println("请输入wsad来进行操作:");
			choice = sc.next();
			
			//根据用户的操作,来移动勇士
			switch( choice ) {
				case "w": move(-10); break; 
				case "s": move(10); break; 
				case "a": move(-1); break; 
				case "d": move(1); break; 
			}
			
			//每次移动完,都要重新显示地图
			createMap();
		}
	}

	//勇士移动位置
	private void move(int val) {
		//首先,先得到这一层的地图
		String[] map = Map.maps[mapIndex - 1];
		//勇士移动之前,要做各种判断
		//先得到移动之后是什么东西
		String temp = map[hindex + val];
		if( "x".equals(temp) ) {
			//撞到墙了   不动
			return;
		}else if("a".equals(temp)) {
			System.out.println("勇士捡到了红宝石,攻击力+3");
			hero.setAtt( hero.getAtt() + 3 );
			System.out.println( hero );
		}else if("d".equals(temp)) {
			System.out.println("勇士捡到了蓝宝石,防御力+3");
			hero.setDef( hero.getDef() + 3 );
			System.out.println( hero );
		}else if("r".equals(temp)) {
			System.out.println("勇士捡到了红血瓶,生命+200");
			hero.setBlood( hero.getBlood() + 200 );
			System.out.println( hero );
		}else if("b".equals(temp)) {
			System.out.println("勇士捡到了蓝血瓶,生命+400");
			hero.setBlood( hero.getBlood() + 400 );
			System.out.println( hero );
		}else if("i".equals(temp)) {
			System.out.println("勇士捡到了黄钥匙,黄钥匙+1");
			hero.setyKey( hero.getyKey() + 1 );
			System.out.println( hero );
		}else if("m".equals(temp)) {
			//先看你有没有钥匙
			if( hero.getyKey() <= 0 ) {
				System.out.println("钥匙数量不够");
				return;
			}
			System.out.println("勇士打开了门,黄钥匙-1");
			hero.setyKey( hero.getyKey() - 1 );
			System.out.println( hero );
		}else if( " ".equals(temp) ) {
			
		}else if("n".equals(temp)) {
			System.out.println("上楼梯");
			mapIndex++;
			//勇士的位置要发生改变
			hindex = upIndex[mapIndex - 1];
			return;
		}else if("p".equals(temp)) {
			System.out.println("下楼梯");
			mapIndex--;
			//勇士的位置要发生改变
			hindex = downIndex[mapIndex - 1];
			return;
		}else {
			//除开上述所有的情况,剩下的就是   数字 了   数字是打怪了
			// String -> int
			int index = Integer.parseInt( temp ) - 1;
			//获取到怪物
			Monster m = Monsters.ms[index];
			System.out.println("遇到怪物: " + m);
			
			fight(m);
			
			System.out.println(hero);
		}
		
		map[hindex] = " ";
		map[hindex + val] = "h";
		hindex += val;
	}

	//打架 在这里,  
	private void fight(Monster m) {
		int mblood = m.getBlood();
		
		//计算勇士攻击怪物
		int h2m = hero.getAtt() - m.getDef();
		if( h2m <= 0 ) {
			System.out.println("勇士砍不动怪物,被怪物乱刀砍死");
			isGameOver = true;
			return;
		}
		//计算怪物攻击勇士
		int m2h = m.getAtt() - hero.getDef();
		m2h = m2h < 0 ? 0 : m2h;
		
		//开打
		do {
			//首先,勇士攻击怪物
			mblood = mblood - h2m;
			if( mblood <= 0 ) {
				System.out.println("战斗胜利,获得"+m.getGold()+"金币,"+m.getExp()+"经验");
				hero.setGold( hero.getGold() + m.getGold() );
				hero.setExp( hero.getExp() + m.getExp() );
				return;
			}
			
			//没打死,那么,怪物,攻击勇士
			hero.setBlood( hero.getBlood() - m2h );
			if( hero.getBlood() <= 0 ) {
				System.out.println("勇士打不过怪物,被怪物乱刀砍死");
				isGameOver = true;
				return;
			}
		}while( hero.getBlood() > 0 && mblood > 0 );
	}

	private void createMap() {
		//首先,先得到这一层的地图
		String[] map = Map.maps[mapIndex - 1];
		for(int i = 0; i < map.length; i++) {
			System.out.print( map[i] + " " );
			//什么时候换行?
			if( (i + 1) % 10 == 0 ) {
				System.out.println();
			}
		}
	}
	
}
